<?php echo "<?php"; ?>

namespace App\Http\Controllers;

use App\Models\{{$model_name}};
use App\Http\C5\C5ResponseFactory;
use App\Models\Province;
use Illuminate\Support\Facades\DB;

class {{$class}} extends BaseController
{

    public function edit(){
        {{$dollar}}id = $this->get("{{$primary_key}}");
        //如有需要这里加上排重的功能
        if(empty({{$dollar}}id)){
            return $this->create();
        }else{
            return $this->update();
        }
    }

    public function create()
    {
        {{$dollar}}bean = new {{$model_name}}();
        $this->markCreateInfo($bean);
        {{$dollar}}bean->forceFill({{$dollar}}this->request->all());
        {{$dollar}}bean->save() ;
        return C5ResponseFactory::newSuccessInstance({{$dollar}}bean);
    }

    public function update()
    {
        /**
        * @var {{$dollar}}bean {{$model_name}}
        */
        {{$dollar}}{{$primary_key}} = $this->get("{{$primary_key}}");
        {{$dollar}}bean = {{$model_name}}::find({{$dollar}}{{$primary_key}});
        if (empty({{$dollar}}bean)) {
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        {{$dollar}}args = {{$dollar}}this->request->all();
        $this->markUpdateInfo($bean);
        {{$dollar}}bean->forceFill({{$dollar}}args);
        {{$dollar}}bean->update() ;
        return C5ResponseFactory::newSuccessInstance({{$dollar}}bean);
    }

    public function delete()
    {
        /**
        * @var {{$dollar}}bean {{$model_name}}
        */

        {{$dollar}}{{$primary_key}} = {{$dollar}}this->get("{{$primary_key}}");
        {{$dollar}}bean = {{$model_name}}::find({{$dollar}}{{$primary_key}});
        if (empty({{$dollar}}bean)) {
            return C5ResponseFactory::newSuccessInstance({{$dollar}}{{$primary_key}});
        }
        // 检查数据完整性,如果有被使用就不可以删除

        // {{$dollar}}object = SomeModel::where("{{$primary_key}}", {{$dollar}}{{$primary_key}})->first();
        // if (!empty({{$dollar}}object)) {
        //      return C5ResponseFactory::newRecordUsedInstance();
        // }

        {{$dollar}}bean->delete();
        // 删除关联的数据
        // SomeModel::on($db)->where("{{$primary_key}}", {{$dollar}}{{$primary_key}})->delete();

        return C5ResponseFactory::newSuccessInstance({{$dollar}}{{$primary_key}});
    }

    public function detail(){
        {{$dollar}}id = $this->get("{{$primary_key}}");
        return $this->find({{$dollar}}id);
    }

    public function find({{$dollar}}{{$primary_key}})
    {
        {{$dollar}}sql = "
            select a.*
            from {{$table}} a
            where {{$primary_key}}=?
        ";
        {{$dollar}}result = DB::select({{$dollar}}sql,[{{$dollar}}{{$primary_key}}]);
        if(empty({{$dollar}}result)){
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        return C5ResponseFactory::newSuccessInstance({{$dollar}}result[0]);
    }

    public function grid()
    {
        {{$dollar}}sql = "
            select a.* from {{$table}} a
            where 1=1
        ";
        {{$dollar}}sql .= {{$dollar}}this->getQueryLimits();
        {{$dollar}}sql .= {{$dollar}}this->getOrderBy(["{{$primary_key}}"=>"desc"]);
        {{$dollar}}result = {{$dollar}}this->gridQuery({{$dollar}}sql);
        return C5ResponseFactory::newSuccessInstance({{$dollar}}result);
    }


}

